package database

import (
	"errors"
	"yoga/graph/model"

	"gorm.io/gorm"
)

func (user User) ToPublicUser() *model.User {
	return &model.User{
		ID:       int32(user.ID),
		UserName: user.UserName,
		Auth:     user.Auth,
		Gender:   user.Gender,
		Age:      int32(user.Age),
	}
}
func (user User) ToPrivateUser() *model.Me {
	return &model.Me{
		ID:       int32(user.ID),
		UserName: user.UserName,
		Auth:     user.Auth,
		Gender:   user.Gender,
		Age:      int32(user.Age),
		Account:  user.Account,
		Phone:    user.Phone,
	}
}

func VerifyAccountAndPasswordGetUserIDAndAuth(account string, password string) (int, string, error) {
	var user User
	err := DB.Where("account = ?", account).First(&user).Error
	if err == gorm.ErrRecordNotFound {
		return 0, "", errors.New("账号不存在")
	}
	if user.Password != password {
		return 0, "", errors.New("密码错误")
	}
	return user.ID, user.Auth, nil
}
func GetUserByID(id int) (*User, error) {
	var user User
	err := DB.Where("id = ?", id).First(&user).Error
	if err == gorm.ErrRecordNotFound {
		return nil, errors.New("用户不存在")
	}
	return &user, nil
}
